package ke
{
	import com.standlibrary.load.QueueLoader;
	import com.standlibrary.load.SLoader;
	
	import flash.display.Sprite;
	import flash.display.StageQuality;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.system.ApplicationDomain;
	import flash.utils.ByteArray;
	import flash.utils.getTimer;

	[SWF(width="780", height="550", frameRate="24", backgroundColor="0x000000")]
	public class ke extends Sprite
	{
		
		private var loadingMC:LoadingMC;
		private var loader:QueueLoader;
		private var xmlXML:XMLList;
		
		public function ke()
		{
			addEventListener(Event.ADDED_TO_STAGE, addToStage);
		}
		
		private function addToStage(eve:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, addToStage);
			
			//init stage
			setStage();

			//init loaderbar
			addLoaderBar();
			
			//load resource
			loadconfig();
		}
		
		private function setStage():void
		{
			stage.quality = StageQuality.HIGH;
			stage.scaleMode = StageScaleMode.NO_SCALE;
		}
		
		private function addLoaderBar():void
		{
			loadingMC = new LoadingMC();
			addChild(loadingMC);
		}
		
		private function loadconfig():void
		{
			var sloader:SLoader = new SLoader();
			sloader.loadOther("config/resource.xml"+"?"+Math.random()+""+getTimer());
			sloader.completeFunc = loadSWF;
		}
		
		private function loadSWF(_byt:ByteArray):void
		{
			var xml:XML = new XML(_byt);
			
			var swfXML:XMLList = xml.swf;
			xmlXML = xml.xml;
			
			var paths:Vector.<String> = new Vector.<String>;
			for(var i:int=0, m:int=swfXML.length(); i<m; i++){
				paths.push(swfXML[i].@path +"?"+ swfXML[i].@version);
			}
			
			loader = new QueueLoader();
			loader.load(paths);
			loader.addEventListener(Event.COMPLETE, swfLoaded, false, 0, true);
			loader.addEventListener(ProgressEvent.PROGRESS, progressLoad, false, 0, true);
		}
		
		private function progressLoad(eve:ProgressEvent):void
		{
			loader.index;
			loader.total;
			loadingMC.NumTxt.text = "";
		}
		
		private function swfLoaded(eve:Event):void
		{
			loader.removeEventListener(Event.COMPLETE, swfLoaded);
			loader.removeEventListener(ProgressEvent.PROGRESS, progressLoad)
			loader = null;
			
			for(var i:int=0, m:int=xmlXML.length(); i<m; i++)
			{
				var loader:SLoader = new SLoader();
				loader.loadOther(xmlXML[i].@path +"?"+ xmlXML[i].@version);
				loader.completeFunc = txtLoaded;
			}
		}
		
		private var txtIndex:int=0;
		private function txtLoaded(byt:ByteArray):void
		{
			txtIndex ++;
			
			var xml:XML = new XML(byt);
			
			if(txtIndex == xmlXML.length()){
				startGame();
			}
			
//			var Obj:Object=getDefinitionByName(c);
//			if(Obj.hasOwnProperty(func)){
//				Obj[func](xml);
//			}
//			else{
//				Obj.getInstance()[func](xml);
//			}
		}
		
		private function startGame():void
		{
			var c:Class = ApplicationDomain.currentDomain.getDefinition("main.main") as Class;
			var main:* = new c();
			stage.addChild(main);
			main.start();
			
			//销毁 ke
			destory();
		}
		
		private function destory():void
		{
			while(numChildren)
			{
				removeChildAt(0);
			}
		}
	}
}










